package com.yh.autobio.modules.adatper;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yh.autobio.R;
import com.yh.autobio.common.StringUtils;
import com.yh.autobio.gddb.gbmanager.BackMatManager;
import com.yh.autobio.gddb.gbmanager.MatHeadManager;
import com.yh.autobio.gddb.gdbean.BackMatBean;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：john
 * <p>
 * 工程：FoodBarcode
 * <p>
 * 文件名：BackMatAdapter
 * <p>
 * 创建时间：2017.05.27.14:40
 * *****************************
 */
public class BackMatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private ArrayList<BackMatBean> workInfos;

    public BackMatAdapter(Context context, ArrayList<BackMatBean> checkedCKS, OnSaleAddAdapterListern listener) {
        this.mContext = context;
        this.workInfos = checkedCKS;
        this.listener = listener;
    }

    public ArrayList<BackMatBean> getBackList() {
        return workInfos;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_sale_out, parent,
                false);
        return new BackViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        BackMatBean backMatBean = workInfos.get(position);
        initDataView((BackViewHolder) holder, backMatBean, position);
    }

    public void initDataView(final BackViewHolder holder, final BackMatBean bean, final int position) {
        String value = MatHeadManager.getInstance(mContext).getHeadValue("发货仓库", "3");
        double v =0;

        BackMatBean tdcStockByID = BackMatManager.getInstance(mContext).getTDCStockByID(bean.getId());
        if (!StringUtils.isEmpty(tdcStockByID.getTDCStockName())) {
            holder.tvFhWare.setText(tdcStockByID.getTDCStockName());//发货仓库
        } else {
            holder.tvFhWare.setText(value);//发货仓库
        }
        if ("0".equals(bean.getUnitType())){//0代表baseName 1代表unit_name
            holder.tvUnit.setText(bean.getBaseName());
            holder.tvSalesSingle.setText(bean.getReprice() + "");
            v= Integer.parseInt(bean.getCount()) * bean.getReprice();
        }else if ("1".equals(bean.getUnitType())){
            holder.tvUnit.setText(bean.getUnit_name());
            holder.tvSalesSingle.setText(bean.getSaleprice() + "");
            v= Integer.parseInt(bean.getCount()) * bean.getSaleprice();
        }
        holder.tvCcCode.setText(bean.getNumber());
        holder.tvSfNum.setText(bean.getCount());
        holder.tvGgModle.setText(bean.getModel());


        holder.tvCcName.setText(bean.getName());
        holder.tvSalesMoney.setText(v + "");
        holder.llJF.setVisibility(View.GONE);
        holder.llBz.setVisibility(View.GONE);
//        holder.tvFhWare.setText(value);//发货仓库
        holder.tvSfNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String text = editable.toString().trim();
                if (StringUtils.isEmpty(text)) {
                    bean.setCount("0");
                    BackMatManager.getInstance(mContext).updateItemCount(bean);
                    holder.tvSfNum.setText("0");
                    holder.tvSalesMoney.setText("0");
                } else {
                    bean.setCount(text);
                    BackMatManager.getInstance(mContext).updateItemCount(bean);
                    int i = Integer.parseInt(text);
                    if ("0".equals(bean.getUnitType())){//0代表baseName 1代表unit_name
                        holder.tvSalesMoney.setText((bean.getReprice() * i) + "");
                    }else if ("1".equals(bean.getUnitType())){
                        holder.tvSalesMoney.setText((bean.getSaleprice() * i) + "");
                    }
                }

            }
        });
        holder.llFhck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onFHCKClick(bean);
                }
            }
        });
        holder.llDw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onDwClick(bean,holder.tvUnit);
                }
            }
        });
    }

    private OnSaleAddAdapterListern listener;

    public interface OnSaleAddAdapterListern {
        void onFHCKClick(BackMatBean position);
        void onDwClick(BackMatBean position, TextView tvUnit);
    }

    @Override
    public int getItemCount() {
        return workInfos.size();
    }

    class BackViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.tv_cc_code)
        TextView tvCcCode;
        @InjectView(R.id.tv_cc_name)
        TextView tvCcName;
        @InjectView(R.id.tv_gg_modle)
        TextView tvGgModle;
        @InjectView(R.id.tv_unit)
        TextView tvUnit;
        @InjectView(R.id.tv_sf_num)
        EditText tvSfNum;
        @InjectView(R.id.tv_sales_single)
        TextView tvSalesSingle;
        @InjectView(R.id.tv_sales_money)
        TextView tvSalesMoney;
        @InjectView(R.id.tv_fh_ware)
        TextView tvFhWare;
        @InjectView(R.id.ll_fhck)
        LinearLayout llFhck;
        @InjectView(R.id.tv_position)
        TextView tvPosition;
        @InjectView(R.id.tv_note)
        TextView tvNote;

        @InjectView(R.id.ll_jf)
        LinearLayout llJF;
        @InjectView(R.id.ll_bz)
        LinearLayout llBz;
        @InjectView(R.id.ll_dw)
        LinearLayout llDw;

        public BackViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }
}
